package com.aob.controller;

import com.alibaba.fastjson.JSON;
import com.aob.pojo.Customer;
import com.aob.service.CustomerService;
import com.aob.tools.SecurityUtil;
import com.aob.vo.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @RequestMapping(value = "/insertEmailCustomer",method = RequestMethod.POST)
    @ResponseBody
    public String insertEmailCustomer(String customerEmail,String customerPwd){
        BaseResult baseResult = new BaseResult();
        Customer customer = new Customer();
        customer.setCustomerId(0);
        String ms = String.valueOf(System.currentTimeMillis()).substring(10);
        customer.setCustomerName("用户"+ SecurityUtil.getRandomString(6)+ms);
        customer.setCustomerEmail(customerEmail);
        customer.setCustomerPwd(customerPwd);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        customer.setCustomerRegTime(sdf.format(new Date()));

        if(customerService.add(customer)){
            baseResult.setMessage("邮箱用户添加成功!");
            baseResult.setStatus("200");
            baseResult.setData(true);
        }else{
            baseResult.setMessage("邮箱用户添加失败!");
            baseResult.setStatus("200");
            baseResult.setData(false);
        }
        return JSON.toJSONString(baseResult);
    }

    @RequestMapping(value = "/insertPhoneCustomer",method = RequestMethod.POST)
    @ResponseBody
    public String insertPhoneCustomer(String customerTel){
        BaseResult baseResult = new BaseResult();
        Customer customer = new Customer();
        customer.setCustomerId(0);
        String ms = String.valueOf(System.currentTimeMillis()).substring(10);
        customer.setCustomerName("用户"+ SecurityUtil.getRandomString(6)+ms);
        customer.setCustomerTel(customerTel);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        customer.setCustomerRegTime(sdf.format(new Date()));
        if(customerService.add(customer)){
            baseResult.setMessage("手机用户添加成功");
            baseResult.setStatus("200");
            baseResult.setData(true);
        }else{
            baseResult.setMessage("手机用户添加成功!");
            baseResult.setStatus("200");
            baseResult.setData(false);
        }
        return JSON.toJSONString(baseResult);
    }


    @RequestMapping(value = "/checkEmail",method = RequestMethod.POST)
    @ResponseBody
    public String checkEmail(String customerEmail){
        BaseResult baseResult = new BaseResult();
        if(customerService.checkEmail(customerEmail)){
            baseResult.setMessage("邮箱已被注册!");
            baseResult.setStatus("233");
            baseResult.setData(false);
        }else{
            baseResult.setMessage("邮箱可用!");
            baseResult.setStatus("200");
            baseResult.setData(true);
        }
        return JSON.toJSONString(baseResult);
    }

    @RequestMapping(value = "/checkPhone",method = RequestMethod.POST)
    @ResponseBody
    public String checkPhone(String customerTel){
        BaseResult baseResult = new BaseResult();
        if(customerService.checkTel(customerTel)){
            baseResult.setMessage("手机号已被注册!");
            baseResult.setStatus("233");
            baseResult.setData(false);
        }else{
            baseResult.setMessage("手机号可用!");
            baseResult.setStatus("200");
            baseResult.setData(true);
        }
        return JSON.toJSONString(baseResult);
    }
}
